local skel = fk.CreateSkill {
  name = "emo__lihen",
}

skel:addEffect(fk.Death, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, false, true)
  end,
  on_cost = function (self, event, target, player, data)
    local tos = player.room:askToChoosePlayers(player, {
      targets = player.room.alive_players,
      min_num = 1, max_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#emo__lihen-choose",
    })
    if #tos > 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    room:setPlayerMark(to, "@@emo__lihen", 1)
  end,
})

skel:addEffect(fk.EventPhaseStart, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return target == player and target.phase == Player.Play and player:getMark("@@emo__lihen") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards, map = {}, {}
    for _, id in ipairs(target:getCardIds("he")) do
      if not target:prohibitDiscard(Fk:getCardById(id)) then
        local t = Fk:getCardById(id).type
        map[t] = map[t] or {}
        table.insert(map[t], id)
      end
    end
    for _, c in pairs(map) do
      table.insert(cards, table.random(c))
    end
    if #cards > 0 then
      room:throwCard(cards, skel.name, target, target)
    end
    if not target.dead and #cards < 3 then
      room:loseHp(target, 1, skel.name)
    end
  end,
})

Fk:loadTranslationTable{
  ["emo__lihen"] = "离恨",
  [":emo__lihen"] = "当你死亡时，你可以令一名角色的每个出牌阶段开始时随机弃置每种类型各一张牌，若弃牌少于三张，其失去一点体力。",

  ["#emo__lihen-choose"] = "离恨：你可选择1名角色，其出牌阶段随机弃置每种类型各一张牌",
  ["@@emo__lihen"] = "离恨",
}


return skel
